package net.javastone.toolkit.robot.dingding.resource;

import net.javastone.toolkit.robot.dingding.model.DingDingResponse;
import net.javastone.toolkit.robot.dingding.model.GitlabResponse;
import net.javastone.toolkit.robot.dingding.model.GitlabWikiRequest;
import net.javastone.toolkit.robot.dingding.service.DingDingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by sdliang on 2018/11/6.
 */
@RestController
@RequestMapping("/gitlab")
public class GitLabWikiResource extends GitLabWebHookResource {

  @Autowired(required = false)
  private DingDingService dingDingService;

  /**
   * 转发gitlab的wiki请求给钉钉
   *
   * @param accessToken
   * @param wiki
   * @return
   */
  @PostMapping(value = "/wiki",
               consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
               produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity<GitlabResponse> wiki(@RequestParam("access_token") String accessToken,
                                             @RequestBody GitlabWikiRequest wiki) {
    DingDingResponse dingResponse = dingDingService.gitlabWiki(accessToken, of(wiki));
    return ResponseEntity.ok(to(dingResponse));
  }

}
